<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */ 

if ($param['op']=="save") {
	$pref = new preferences;
	$pref->userid = $CURRENT_USER->userid;
	$pref->id = $param['id'];
	$r = $loader->get($pref);

	switch ($param['type']) {
		case "bool":
			$def = $PREF_LIST[$param['id']]['default'];
			if ($def=="true") {
				if ($param['value']=="true") {
					$loader->delete($pref);
				} else {
					$loader->save($pref);		
				}
			} else {
				if ($param['value']=="true") {
					$loader->save($pref);
				} else {
					$loader->delete($pref);		
				}				
			}
			break;
		default:
			$pref->value=$param['value'];
			if ($r) {
				$loader->update($pref);
			} else {
				$loader->save($pref);
			}
			break;
	}
	success();
} else {
	$pref = new preferences;
	$pref->userid=$CURRENT_USER->userid;
	$list=$loader->loadList($pref);

	$res ="{results:[";
	$act = "";
	foreach ($PREF_LIST as $pr) {
		$_log->debug($pr);
		if ($pr['group']=="*" or in_array($CURRENT_USER->groupid,$pr['group'])) {
			if ($act!="") $act.=", ";
			$act .= "{id: '".$pr['code']."', " .
					"text: '".s($pr['text'])."', " .
					"type: '".$pr['type']."', " . 
					"value: ";
			switch ($pr['type']) {
				case "bool":
					$exists = array_key_exists($pr['code'],$list);
					if ($pr['default']=="true") {
						$v = (!$exists ? "true" : "false");	
					} else {
						$v = ($exists ? "true" : "false");
					}
					$act.="'".$v."'";
					break;
				default:
					if (array_key_exists($pr['code'],$list)) {
						$act .= "'".$list[$pr['code']]."'";
					} else {
						$act .= "'".$pr['default']."'";
					}
					break;
			}
			$act.="}";
		} 
	}
	$res .= $act."]}";
	out($res);	
}

?>

 